package com.jsp.handler;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

//在发生异常后，会将异常信息储存进request里进行转发，所以request里会多一些信息
public class ErrorHandler extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        try{
        //状态码
        Integer statusCode =(Integer) req.getAttribute("javax.servlet.error.status_code");
        //异常对应的Class类
        Class type=(Class)req.getAttribute("javax.servlet.error.exception_type");
        //
        String message=(String)req.getAttribute("javax.servlet.error.message");
        //
        String uri=(String)req.getAttribute("javax.servlet.error.request_uri");
        //异常类
        Throwable exception=(Throwable)req.getAttribute("javax.servlet.error.exception");
        //发出异常的servlet名字，不带包名
        String name=(String)req.getAttribute("javax.servlet.error.servlet_name");

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write("<h1>状态码: "+statusCode+"</h1>");
        writer.write("<p>信息："+message+"</p>");
        } catch (IOException e) {
            e.printStackTrace();
        }}
    @Override
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response){
        doGet(request, response);
    }

}
